#include "robot_comm.h"
#include <iostream>

// RobotComm* RobotComm::only_instance = NULL;
// string RobotComm::control_version = std::string(__DATE__);

RobotComm::RobotComm()
{
	manage = new el::CommServer;
	manage->start();
	modbustcp_client_ = new ModbusTcpClient(manage);
 	modbustcp_client_vision_ = new ModbusTcpClientVision(manage);
	modbustcp_server_ = new ModbusTcpServer(manage);
	remotetcp_server_ = new RemoteTcpServer(manage);
	gibbon_client_ = new RemoteGibbon(manage);

}

RobotComm::~RobotComm()
{
	delete modbustcp_client_;
 	delete modbustcp_client_vision_;
	delete gibbon_client_;
	delete modbustcp_server_;
	delete remotetcp_server_;
	for (int i=0;i<server_list.size();i++)
	{
		delete server_list.at(i);
	}
	for (int i=0;i<modbus_server_list.size();i++)
	{
		delete modbus_server_list.at(i);
	}
	for (int i=0;i<client_list.size();i++)
	{
		delete client_list.at(i);
	}
	manage->stop();
	Sleep(200);
	delete manage;
}


HANDLE RobotComm::addTcpServer( STcpServerInfo *info_/*=NULL*/ )
{
	bool result = false;
	TcpServerNormal *server_ = new TcpServerNormal(manage,info_,&result);
	if (result)
	{
		server_list.push_back(server_);
		return (HANDLE)server_;
	}
	delete server_;
	return NULL;
}

bool RobotComm::removeTcpServer( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<server_list.size();i++)
	{
		if (server_list.at(i) == handle_)
		{
			delete server_list.at(i);
			server_list.erase(server_list.begin()+i,server_list.begin()+i+1);
			return true;
		}
	}
	return false;
}

int RobotComm::startTcpServer( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<server_list.size();i++)
	{
		if (server_list.at(i) == handle_)
		{
			return server_list.at(i)->startTcpServer();
		}
	}
	return -1;
}
int RobotComm::disConnectToOneClient( HANDLE handle_,HANDLE connect_/*=NULL*/ )
{
	for (int i=0;i<server_list.size();i++)
	{
		if (server_list.at(i) == handle_)
		{
			return server_list.at(i)->disConnectToClient(connect_);
		}
	}
	return -1;
}


bool RobotComm::getTcpServerConnectList( HANDLE handle_,vector<STcpConnectClientInfo> *list)
{
	for (int i=0;i<server_list.size();i++)
	{
		if (server_list.at(i) == handle_)
		{
			*list = server_list.at(i)->getTcpServerConnectList();
			return true;
		}
	}
	return false;
}

int RobotComm::serverSendData( HANDLE handle_/*=NULL*/,HANDLE connect_/*=NULL*/,char *data_/*=NULL*/,int data_len/*=0*/ )
{
	for (int i=0;i<server_list.size();i++)
	{
		if (server_list.at(i) == handle_)
		{
			return server_list.at(i)->serverSendData(connect_,data_,data_len);
		}
	}
	return -1;
}

int RobotComm::serverReadData( HANDLE handle_/*=NULL*/,HANDLE connect_/*=NULL*/,char *data_/*=NULL*/ )
{
	for (int i=0;i<server_list.size();i++)
	{
		if (server_list.at(i) == handle_)
		{
			return server_list.at(i)->serverReadData(connect_,data_);
		}
	}
	return -1;
}

HANDLE RobotComm::addModbusTcpServer( SModbusTcpServerNormalInfo *info_/*=NULL*/ )
{
	bool result = false;
	ModbusTcpServerNormal *modbus_server_ = new ModbusTcpServerNormal(manage,info_,&result);
	if (result)
	{
		modbus_server_list.push_back(modbus_server_);
		return (HANDLE)modbus_server_;
	}
	delete modbus_server_;
	return NULL;
}

bool RobotComm::removeModbusTcpServer( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<modbus_server_list.size();i++)
	{
		if (modbus_server_list.at(i) == handle_)
		{
			delete modbus_server_list.at(i);
			modbus_server_list.erase(modbus_server_list.begin()+i,modbus_server_list.begin()+i+1);
			return true;
		}
	}
	return false;
}

int RobotComm::startModbusTcpServer( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<modbus_server_list.size();i++)
	{
		if (modbus_server_list.at(i) == handle_)
		{
			return modbus_server_list.at(i)->startModbusTcpServer();
		}
	}
	return -1;
}

bool RobotComm::getModbusTcpServerConnectList( HANDLE handle_/*=NULL*/,vector<STcpConnectClientInfo> *list/*=NULL*/ )
{
	for (int i=0;i<modbus_server_list.size();i++)
	{
		if (modbus_server_list.at(i) == handle_)
		{
			*list = modbus_server_list.at(i)->getModbusTcpServerConnectList();
			return true;
		}
	}
	return false;
}

HANDLE RobotComm::addTcpClient( STcpServerInfo *info_/*=NULL*/ )
{
	bool result = false;
	TcpClientNormal *client_ = new TcpClientNormal(manage,info_,&result);
	if (result)
	{
		client_list.push_back(client_);
		return (HANDLE)client_;
	}
	delete client_;
	return NULL;
}

bool RobotComm::removeTcpClient( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<client_list.size();i++)
	{
		if (client_list.at(i) == handle_)
		{
			delete client_list.at(i);
			client_list.erase(client_list.begin()+i,client_list.begin()+i+1);
			return true;
		}
	}
	return false;
}

bool RobotComm::getTcpClientStatus( HANDLE handle_/*=NULL*/,STcpClientStatus *status_/*=NULL*/ )
{
	for (int i=0;i<client_list.size();i++)
	{
		if (client_list.at(i) == handle_)
		{
			return client_list.at(i)->getTcpClientStatus(status_);
		}
	}
	return false;
}

int RobotComm::connectToServer( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<client_list.size();i++)
	{
		if (client_list.at(i) == handle_)
		{
			return client_list.at(i)->connectToServer();
		}
	}
	return -1;
}

int RobotComm::disConnectToServer( HANDLE handle_/*=NULL*/ )
{
	for (int i=0;i<client_list.size();i++)
	{
		if (client_list.at(i) == handle_)
		{
			return client_list.at(i)->disConnectToServer();
		}
	}
	return -1;
}

int RobotComm::clientSendData( HANDLE handle_/*=NULL*/,char *data_/*=NULL*/,int data_len/*=0*/ )
{
	for (int i=0;i<client_list.size();i++)
	{
		if (client_list.at(i) == handle_)
		{
			return client_list.at(i)->clientSendData(data_,data_len);
		}
	}
	return -1;
}

int RobotComm::clientReadData( HANDLE handle_/*=NULL*/,char *data_/*=NULL*/ )
{
	for (int i=0;i<client_list.size();i++)
	{
		if (client_list.at(i) == handle_)
		{
			return client_list.at(i)->clientReadData(data_);
		}
	}
	return -1;
}

